//
//  XQBaseViewController.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/20.
//

#import "XQBaseViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface XQBaseViewController ()<CLLocationManagerDelegate>

@property (strong, nonatomic) CLLocationManager *locationManager;

@end

@implementation XQBaseViewController

- (void)dealloc {
    NSLog(@"%@ dealloc", NSStringFromClass([self class]));
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.modalPresentationStyle = UIModalPresentationFullScreen;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.navigationBar];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self removeNotifications];
    [self addNotifications];
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self removeNotifications];
}

- (void)onBack {
    [self.view endEditing:YES];
    
    if (self.navigationController) {
        if (self.navigationController.viewControllers.count > 1) {
            [self.navigationController popViewControllerAnimated:YES];
        } else if (self.navigationController.presentingViewController) {
            [self.navigationController dismissViewControllerAnimated:YES completion:nil];
        }
    } else if (self.presentingViewController) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)startLocation {
    // 开始定位
    [self stopLocation];
    [self.locationManager startUpdatingLocation];
}

- (void)stopLocation {
    // 停止定位以节省电池
    [self.locationManager stopUpdatingLocation];
}


- (void)addNotifications {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(applicationDidBecomeActive:)
                                                 name:UIApplicationDidBecomeActiveNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(applicationEnterBackground:)
                                                 name:UIApplicationDidEnterBackgroundNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(applicationDidEnterForeground:)
                                                 name:UIApplicationWillEnterForegroundNotification
                                               object:nil];
}

- (void)removeNotifications {
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIApplicationDidBecomeActiveNotification
                                                  object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIApplicationDidEnterBackgroundNotification
                                                  object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIApplicationWillEnterForegroundNotification
                                                  object:nil];
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    [manager stopUpdatingLocation];
    CLLocation *currentLocation = [locations lastObject];
    // 获取经纬度
    CLLocationCoordinate2D coordinate = currentLocation.coordinate;

    self.longitude = coordinate.longitude;
    self.latitude = coordinate.latitude;
}

#pragma mark - Application LifeCycle

- (void)applicationDidBecomeActive:(NSNotification *)notification {
    
}

- (void)applicationEnterBackground:(NSNotification *)notification {
    
}

- (void)applicationDidEnterForeground:(NSNotification *)notification {
    
}

#pragma mark - setter

- (void)setTopTitle:(NSString *)topTitle {
    _topTitle = topTitle;
}

#pragma mark - lazy

- (XQNavigatorBar *)navigationBar {
    if (!_navigationBar) {
        _navigationBar = [[XQNavigatorBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].width, [UIScreen mainScreen].topHeight)];
        _navigationBar.backgroundColor = [UIColor whiteColor];
        @weakify(self)
        _navigationBar.backHandle = ^{
            @strongify(self);
            [self onBack];
        };
    }
    return _navigationBar;
}

- (CLLocationManager *)locationManager {
    if (!_locationManager) {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        _locationManager.distanceFilter = kCLDistanceFilterNone;
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    }
    return _locationManager;
}

@end
